﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayPanel : BasePanelUI
{
    #region UILogic
    public Text debugText;
    public Button backButton;
    public Button replayButton;
    public Button tipButton;
    public Text levelText;
    public void Show(int levelId)
    {
        curLevelId = levelId;
        GameCtrl.Instance.loadingPanel.Show(curLevelId, () => { LoadLevel(); });

    }

    private void FinalShow()
    {
        base.Show();
        GameCtrl.Instance.loadingPanel.Hide();
        GameCtrl.Instance.ChangeGameState(GameState.Play);
        levelText.text = curLevelId.ToString();
        //UmengSDK.Instance.StartLevel(curLevelId.ToString());

        if (Main.Instance.isDebug && Application.platform == RuntimePlatform.WindowsEditor)
        {
            //string debugStr = string.Format("{2}_{0}\n{1}", curLevelData.levelName, Localization.Instance.GetString(curLevelData.levelName), curLevelData.id);
            //debugText.text = debugStr;
            //Invoke("GetScreenShot", 1f);
        }
    }

    private void GetScreenShot()
    {
        //string.Format("{2}_{0}{1}.png", curLevelData.levelName, Localization.Instance.GetString(curLevelData.levelName), curLevelData.id);//
        string fileName = string.Format("{1}_{0}.png", curLevelData.levelName, curLevelData.id);
        ScreenCapture.CaptureScreenshot(fileName);
        Invoke("NextLevel", 0.5f);
    }
    public override void Init()
    {
        base.Init();
        rootRect.gameObject.SetActive(false);
    }
    public override void BtnEvent()
    {
        base.BtnEvent();
        backButton.onClick.AddListener(() =>
        {
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            GameCtrl.Instance.levelPanel.Show();
            Hide();
        });
        replayButton.onClick.AddListener(() =>
        {
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            Replay();
        });
        tipButton.onClick.AddListener(() =>
        {
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            Debug.Log("level name : " + curLevelData.levelName);
            string tip = Localization.Instance.GetString(curLevelData.levelName);
            GameCtrl.Instance.tipPanel.Show(tip);
        });
    }
    #endregion

    #region GamePlayLogic
    public Transform levelRoot;
    private int curLevelId;
    private LevelData curLevelData;
    private GameLevelItem curGameLevelItem;
    public bool isGetTip;

    private int notShowAdCount = 0;
    private int showAdRate = 3;

    private void ReleaseGameLevelItem()
    {
        if (null != curGameLevelItem)
            GameObject.Destroy(curGameLevelItem.gameObject);
        curGameLevelItem = null;
        isGetTip = false;
    }

    public void LoadLevel()
    {

        ShowInterAd();
        ReleaseGameLevelItem();
        EnablePlayPanelBtn(true);
        curLevelData = GameCtrl.Instance.GetLevelData(curLevelId);
        if (null == curLevelData)
        {
            Debug.LogError("不存在levelid 为：" + curLevelId + "  的关卡数据");
        }
        Debug.Log("-------------level prefab name :" + curLevelData.levelName);
        GameObject levelObj = Resources.Load("Level/" + curLevelData.levelName) as GameObject;
        curGameLevelItem = GameObject.Instantiate(levelObj).GetComponent<GameLevelItem>();
        ResetUIButtonShow();
        curGameLevelItem.levelId = curLevelId;
        curGameLevelItem.transform.parent = levelRoot;
        curGameLevelItem.transform.localPosition = Vector3.zero;
        curGameLevelItem.transform.localScale = Vector3.one;
        RectTransform rectTsf = curGameLevelItem.gameObject.GetComponent<RectTransform>();
        rectTsf.sizeDelta = Vector2.zero;
        rectTsf.localPosition = Vector3.zero;
        curGameLevelItem.gameObject.SetActive(true);
        Invoke("FinalShow", 1f);
    }

    public void NextLevel()
    {
        if (curLevelId + 1 > GameCtrl.Instance.levelDataList.Count)
        {
            // 已经是最后一关了
            GameCtrl.Instance.finalLevelPanel.Show();
            Hide();
            return;
        }
        if (curLevelId + 1 <= GameCtrl.Instance.levelDataList.Count)
            Show(curLevelId + 1);
    }

    public void Replay()
    {
        Show(curLevelId);
    }

    public void SkipLevel()
    {
        curGameLevelItem.Skip();
    }

    private void ResetUIButtonShow()
    {
        if (null != curGameLevelItem)
        {
            replayButton.gameObject.SetActive(!curGameLevelItem.hideReplayButton);
            backButton.gameObject.SetActive(!curGameLevelItem.hideBackButton);
            levelText.gameObject.SetActive(!curGameLevelItem.hideLevelId);
        }
    }

    private void ShowInterAd()
    {
        if (notShowAdCount < showAdRate)
        { notShowAdCount++; }
        else
        {
            notShowAdCount = 0;
            SDKManagerV2.Instance.ShowInterAd();
        }
    }


    public void EnablePlayPanelBtn(bool enable)
    {
        backButton.enabled = enable;
        tipButton.enabled = enable;
        replayButton.enabled = enable;
    }

    #endregion


    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.N))
        {
            if (Main.Instance.isDebug && Application.platform == RuntimePlatform.WindowsEditor)
            {
                GameCtrl.Instance.UpdateLevelState(curLevelId, UConst.Level_State_Finish);
                NextLevel();
            }
        }
    }
}
